import Axios from 'axios'
import {Notification} from 'element-ui'

function _callback (response, callback, errorCallback) {

  if (response.data.rspCode != undefined) {
    if (response.data.rspCode == 0) {
      callback(response.data.data)
    } else {
      if (response.data.rspCode == 1212) {
        goLogin()

      } else if (response.data.rspCode == 1213) {
        window.location.href = response.data.rspMsg
      } else if(response.data.rspCode == 999){
        Notification.warning({
          title: '友情提示',
          message: response.data.rspMsg
        })
      }else {
        Notification.error({
          title: '错误',
          message: response.data.rspMsg
        })
      }
    }
  } else if (response.data.success != undefined) {
    if (response.data.success) {
      callback(response.data.body)
    } else if (!response.data.success) {
      Notification.error({
        title: '错误',
        message: response.data.message
      })
      callback(response.data.body)
    } else {
      Notification.error({
        title: '错误',
        message: response.data.message || '系统错误'
      })
    }
  } else {
    callback(response.data)
  }
}

function _callback01 (response, callback, errorCallback) {
  if (response.data.success) {
    callback(response.data)
  } else {
    /* alert()
    if (response.data.rspCode == 1212) {
      goLogin()
    } else {
      if(response.data.rspCode == 1211){
        Notification.error({
          title: '错误',
          message: '没有权限！'
        })
      }else{
        Notification.error({
          title: '错误',
          message: response.data.message
        })
      }
    } */
  }
}

function _errorCallback (error, errorCallback) {
  if (errorCallback) {
    errorCallback(error)
  } else {
    Notification.error({
      title: '错误',
      message: '错误: ' + error
    })
  }
}

export default {
  request (method, url, params, callback, errorCallback) {
    let config = {
      method: method,
      url: url,
      // timeout: 3000,
      headers: {
        'X-Requested-With': 'XMLHttpRequest'
      },
      withCredentials: true
    }

    let bodyRequestMethod = ['post', 'put', 'patch']
    let bodyReqmethod = ['postResponse', 'getResponse']

    if (~bodyRequestMethod.indexOf(method)) {
      config.data = params
    } else {
      // config.params = params
    }
    Axios.request(config)
      .then(function (response) {
        if (~bodyReqmethod.indexOf(method)) {
          _callback01(response, callback, errorCallback)
        } else {
          _callback(response, callback, errorCallback)
        }
      })
      .catch(function (error) {
        _errorCallback(error, errorCallback)
      })
  },
  get (url, params, callback, errorCallback) {
    if (process.env.NODE_ENV != 'development') {
      url = process.env.WEB_API + url
    }
    let query = ''
    for (let key in params) {
      query += '&' + key + '=' + params[key]
    }
    if (~url.indexOf('?')) {
      url += query
    } else {
      url += '?' + query.slice(1)
    }
    this.request('get', url, params, callback, errorCallback)
  },
  delete (url, params, callback, errorCallback) {
    this.request('delete', url, params, callback, errorCallback)
  },
  head (url, params, callback, errorCallback) {
    this.request('head', url, params, callback, errorCallback)
  },
  options (url, params, callback, errorCallback) {
    this.request('options', url, params, callback, errorCallback)
  },
  post (url, params, callback, errorCallback) {
    if (process.env.NODE_ENV != 'development') {
      url = process.env.WEB_API + url
    }
    this.request('post', url, params, callback, errorCallback)
  },
  postResponse (url, params, callback, errorCallback) {
    this.request('post', url, params, callback, errorCallback)
  },
  put (url, params, callback, errorCallback) {
    this.request('put', url, params, callback, errorCallback)
  },
  patch (url, params, callback, errorCallback) {
    this.request('patch', url, params, callback, errorCallback)
  },
  getResponse (url, params, callback, errorCallback) {
    let query = ''
    for (let key in params) {
      query += '&' + key + '=' + params[key]
    }
    if (~url.indexOf('?')) {
      url += query
    } else {
      url += '?' + query.slice(1)
    }
    this.request('get', url, params, callback, errorCallback)
  },
  open (url, params) {
    let query = ''
    for (let key in params) {
      query += '&' + key + '=' + params[key]
    }
    if (~url.indexOf('?')) {
      url += query
    } else {
      url += '?' + query.slice(1)
    }
    window.open(url)
  },
  postFormOpen (url, formData) {
    var config = {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    }
    Axios.post(url, formData, config).then(function (res) {
      console.log(res)
    })
  }
}
